package com.cennavi.minemap.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.cennavi.minemap.bean.DataBean;
import com.cennavi.minemap.bean.MessageBean;
import com.cennavi.minemap.bean.SoluBean;
import com.cennavi.minemap.bean.SourceBean;
import com.cennavi.minemap.bean.UploadBean;
import com.cennavi.minemap.service.UploadService;
import com.cennavi.minemap.util.Constants;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
@ComponentScan(basePackages = { "com.cennavi.minemap.service" })
@RequestMapping("/service/upload")
public class UploadController {

	ObjectMapper objectMapper = new ObjectMapper();

	@Autowired
	private UploadService uploadService;

	@RequestMapping(value = "/file", method = RequestMethod.POST)
	public @ResponseBody MessageBean upload(@RequestParam("name") String name, @RequestParam("type") int type,
			@RequestParam("userId") int userId, @RequestParam("describe") String describe,
			@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

		MessageBean messageBean = new MessageBean();
		UploadBean uploadBean = new UploadBean();
		uploadBean.setName(name);
		uploadBean.setType(type);
		uploadBean.setFile(file);
		uploadBean.setUserId(userId);
		uploadBean.setDescribe(describe);

		SourceBean sourceBean = new SourceBean();
		sourceBean.setName(name);
		sourceBean.setGroupId(Constants.USER_UPLOAD_GROUP);
		sourceBean.setSource(userId + " upload");

		int uploadId = uploadService.uploadFile(uploadBean, sourceBean);

		List<UploadBean> list = uploadService.queryById(uploadId);

		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("upload error");
		} else {
			DataBean<UploadBean> dataBean = new DataBean<UploadBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/{id}")
	public @ResponseBody MessageBean queryById(@PathVariable int id) {
		MessageBean messageBean = new MessageBean();
		List<UploadBean> list = uploadService.queryById(id);
		if (list == null) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("upload query error");
		} else {
			DataBean<UploadBean> dataBean = new DataBean<UploadBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/list/{userId}")
	public @ResponseBody MessageBean queryByUserId(@PathVariable int userId) {
		MessageBean messageBean = new MessageBean();
		List<UploadBean> list = uploadService.queryByUserId(userId);
		if (list == null) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("upload list query error");
		} else {
			DataBean<UploadBean> dataBean = new DataBean<UploadBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/create/{id}/{uploadId}")
	public @ResponseBody MessageBean createTable(@PathVariable int id, @PathVariable int uploadId) {
		MessageBean messageBean = new MessageBean();
		Integer status = uploadService.createTable(id, uploadId);
		if (status != 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("create error");
		} else {
			messageBean.setErrcode(0);
		}
		return messageBean;
	}

	@RequestMapping(value = "/test/{id}")
	public @ResponseBody MessageBean test(@PathVariable int id) {
		MessageBean messageBean = new MessageBean();
		Integer status = uploadService.test(id);
		if (status != 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("test error");
		} else {
			messageBean.setErrcode(0);
		}
		return messageBean;
	}

	@RequestMapping(value = "/detail/update/{userId}/{uploadId}", method = RequestMethod.POST)
	public @ResponseBody MessageBean update(@PathVariable int userId, @PathVariable int uploadId,
			@RequestBody String json) {
		MessageBean messageBean = new MessageBean();

		Integer status = uploadService.updateDetail(userId, uploadId, JSONObject.parseObject(json));
		if (status != 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("update upload detail error");
		} else {
			List<JSONObject> list = uploadService.queryUploadDetailList(userId, uploadId);
			DataBean<JSONObject> dataBean = new DataBean<JSONObject>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/detail/list/{userId}/{uploadId}")
	public @ResponseBody MessageBean queryUploadDetailList(@PathVariable Integer userId,
			@PathVariable Integer uploadId) {
		MessageBean messageBean = new MessageBean();
		List<JSONObject> list = uploadService.queryUploadDetailList(userId, uploadId);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("query upload detail list error");
		} else {
			DataBean<JSONObject> dataBean = new DataBean<JSONObject>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/detail/byid/{userId}/{uploadId}/{id}")
	public @ResponseBody MessageBean queryUploadDetailById(@PathVariable Integer userId, @PathVariable Integer uploadId,
			@PathVariable Integer id) {
		MessageBean messageBean = new MessageBean();
		List<JSONObject> list = uploadService.queryUploadDetailById(userId, uploadId, id);
		if (list.size() < 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("query upload detail error");
		} else {
			DataBean<JSONObject> dataBean = new DataBean<JSONObject>();
			messageBean.setErrcode(0);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/getSolu/{uploadId}")
	public @ResponseBody MessageBean querySoluId(@PathVariable Integer uploadId) {
		MessageBean messageBean = new MessageBean();

		Integer soluid = uploadService.querySoluId(uploadId);
		if (soluid == null) {
			DataBean<SoluBean> dataBean = new DataBean<SoluBean>();
			messageBean.setErrcode(0);
			dataBean.setTotal(0);
			dataBean.setRows(new ArrayList<SoluBean>());
			messageBean.setData(dataBean);
		} else {
			DataBean<SoluBean> dataBean = new DataBean<SoluBean>();
			messageBean.setErrcode(0);
			List<SoluBean> list = uploadService.queryByIdWithToken(soluid);
			dataBean.setTotal(list.size());
			dataBean.setRows(list);
			messageBean.setData(dataBean);
		}
		return messageBean;
	}

	@RequestMapping(value = "/setSolu", method = RequestMethod.POST)
	public @ResponseBody MessageBean saveSoluId(@RequestBody String text) {
		MessageBean messageBean = new MessageBean();

		int uploadId = JSONObject.parseObject(text).getInteger("uploadId");
		int soluId = JSONObject.parseObject(text).getInteger("soluId");

		Integer status = uploadService.saveSoluId(uploadId, soluId);
		if (status <= 0) {
			messageBean.setErrcode(-1);
			messageBean.setErrmsg("save upload solu error");
		} else {
			messageBean.setErrcode(0);
		}
		return messageBean;
	}
}
